<template>
  <div class="app-container">
  <el-row>
    <el-button @click="showMyBook">显示我的预约</el-button>
    <el-dialog :title="title" :visible.sync="myBookOpen" append-to-body>
      <el-row>
        <el-table v-loading="loading" :data="ownBook">
          <el-table-column label="房间号" align="center" prop="roomId"/>
          <el-table-column label="入住时间" align="center" prop="checkinTime" />
          <el-table-column label="退房时间" align="center" prop="checkoutTime" />
        </el-table>
      </el-row>
      <el-row>
        <el-button @click="cancelMyBook">取消预约</el-button>
        <el-button @click="cancel">返回</el-button>
      </el-row>
    </el-dialog>
  </el-row>

  <el-row>
    <el-table v-loading="loading" :data="roomTypeList">
      <el-table-column label="房间类型" align="center" prop="roomType" v-if="true"/>
      <el-table-column label="房间费用" align="center" prop="price" v-if="true"/>
      <el-table-column label="房间剩余" align="center" prop="roomCount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="openDatePicker(scope.row)"
          >预约</el-button>
          
          <el-dialog :title="title" :visible.sync="open" append-to-body>
            <el-form>

            <el-form-item label="请选择时间">
              <el-date-picker clearable
              v-model="form.timeRange"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择">
              </el-date-picker>
            </el-form-item>

            <el-form-item>
             <el-input v-model="form.serviceRequestContent" type="textarea" placeholder="请在这输入您的服务需求"></el-input>
            </el-form-item>

            <el-form-item>
              <el-button @click="submitForm">确定预约</el-button>
              <el-button @click="cancel">取消</el-button>
            </el-form-item>
          </el-form>

          </el-dialog>


        </template>
      </el-table-column>
    </el-table>
    </el-row>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { getRoomType,submitBook,getMyBook,cancelMyBook } from "@/api/hotel/book";

export default {
  name: "RoomType",
  data() {
    return {
      //个人预约窗口
      myBookOpen:false,
      //弹窗开关
      open:false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      roomTypeList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //个人预约
      ownBook:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询获取房间类型列表 */
    getList() {
      this.loading = true;
      getRoomType().then(response => {
        this.roomTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.myBookOpen=false;
      this.reset();
    },
    //表单重置
    reset() {
      this.form = {
        roomType: undefined,
        checkinTime:undefined,
        checkoutTime:undefined
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
          this.buttonLoading = true;
          if (this.form.roomType != null&&this.form.timeRange !=null) {
            this.form.checkinTime=this.form.timeRange[0];
            this.form.checkoutTime=this.form.timeRange[1];
            submitBook(this.form).then(response => {
              this.$modal.msgSuccess("预约成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
              this.cancel();
            });
          }
    },
    openDatePicker(row){
      this.open=true;
      this.form.roomType= row.roomType;
      this.title="预定详情";
    },
    showMyBook(){
      this.myBookOpen=true;
      this.loading = true;
      getMyBook().then(response =>{
        this.ownBook=response.rows;
        this.total = response.total;
        this.loading = false;
      });
      
     
    },
    cancelMyBook(){
      if(this.ownBook.bookId!=null){
        cancelMyBook();
      }
      else{
        this.$modal.confirm("您现在没有预约");
      }
      this.cancel();
      this.getList();
    }

  }
};
</script>
